package com.hake.test.example;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.hake.example.entity.Resource;

/**
 * 树形结构工具类
 * @author yhaoquan
 *
 */
public class TreeUtil {
	
	/**
	 * 获取所有根节点包括子节点
	 * @param list
	 * @return
	 */
	public List<List<Resource>> getAllRootChildren(List<Resource> list) {
		List<List<Resource>> retList = new ArrayList<List<Resource>>() ;
		for (Resource r : list) {
			if(r != null && r.getParentId() == null) {
				retList.add(getChildHasRoot(list, r.getId()));
			}
		}
		return retList ;
	}
	
	
	/**
	 * 根据父节点的ID获取所有子节点，包含根节点
	 * @param list	具有树形结构特点的集合
	 * @param parent	父节点ID
	 * @return	树形结构集合
	 */
	public List<Resource> getChildHasRoot(List<Resource> list, Integer parentId) {
		List<Resource> returnList = new ArrayList<Resource>();
		
		//取得根节点
		Iterator<Resource> it = list.iterator();
		while (it.hasNext()) {
			Resource n = (Resource) it.next();
			if (n.getId() == parentId) {
				returnList.add(n);
			}
		}
		Resource resource = returnList.get(0) ;
		List<Resource> children = resource.getChildren() ;
		if(null == children) {
			children = new ArrayList<Resource>();
		}
		
		//根据传入的某个父节点ID,遍历该父节点的所有子节点
		for (Iterator<Resource> iterator = list.iterator(); iterator.hasNext();) {
			Resource t = iterator.next();
			if (t.getParentId()==parentId) {
				recursionFn(list, t);
				children.add(t);
			}
		}
		return returnList;
	}
	
	/**
	 * 根据父节点的ID获取所有子节点，不包括根节点
	 * @param list	具有树形结构特点的集合
	 * @param parent	父节点ID
	 * @return	树形结构集合
	 */
	public List<Resource> getChilds(List<Resource> list,Integer parentId) {
		List<Resource> returnList = new ArrayList<Resource>();
		for (Iterator<Resource> iterator = list.iterator(); iterator.hasNext();) {
			Resource t = iterator.next();
			// 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
			if (t.getParentId()==parentId) {
				recursionFn(list, t);
				returnList.add(t);
			}
		}
		return returnList;
	}
	
	/**
	 * 递归列表
	 * @param list
	 * @param t
	 */
	private void recursionFn(List<Resource> list, Resource t) {
		List<Resource> childList = getChildList(list, t);// 得到子节点列表
		t.setChildren(childList);
		for (Resource tChild : childList) {
			if (hasChild(list, tChild)) {// 判断是否有子节点
				Iterator<Resource> it = childList.iterator();
				while (it.hasNext()) {
					Resource n = (Resource) it.next();
					recursionFn(list, n);
				}
			}
		}
	}
	
	/**
	 * 得到子节点列表
	 * @param list
	 * @param t
	 * @return
	 */
	public List<Resource> getChildList(List<Resource> list, Resource t) {
		List<Resource> tlist = new ArrayList<Resource>();
		Iterator<Resource> it = list.iterator();
		while (it.hasNext()) {
			Resource n = (Resource) it.next();
			if(t.getType()!=2)
			{
				if (n.getParentId() == Integer.valueOf(t.getId().toString())) {
					tlist.add(n);
				}
			}
		}
		return tlist;
	} 
	
	/**
	 * 判断是否有子节点
	 * @param list
	 * @param t
	 * @return
	 */
	private boolean hasChild(List<Resource> list, Resource t) {
		return getChildList(list, t).size() > 0 ? true : false;
	}
	
}
